package xyx.ysfwpt.controller;

import io.swagger.annotations.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xyx.ysfwpt.model.Drug;
import xyx.ysfwpt.service.DrugService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ysfwpt")
@Api(tags = {"药品接口"},description = "")
public class DrugController {

    @Autowired
    DrugService drugService;

    @ApiOperation(value = "药品管理",notes = "查询所有药品")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectAllDrug",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectAllOrder(){
        JSONObject jsonObject = new JSONObject();
        List list = drugService.selectAllDrug();
        jsonObject.put("list",list);
        return jsonObject;
    }

    @ApiOperation(value = "药品管理",notes = "根据医院/配送方式/查询词查询药品信息")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectDrugByInputList",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectDrugByInputList(@ApiParam(value = "医院",required = false) @RequestParam(value = "hospital",required = false)String hospital,
                                            @ApiParam(value = "配送方式",required = false) @RequestParam(value = "delivery",required = false)String delivery,
                                            @ApiParam(value = "查询词",required = false) @RequestParam(value = "searchword",required = false)String searchword){
        JSONObject jsonObject = new JSONObject();
        List list = drugService.selectDrugByInputList(hospital,delivery,searchword);
        jsonObject.put("list",list);
        return jsonObject;
    }

    @ApiOperation(value = "新增药品",notes = "新增药品")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "新增成功")})
    @RequestMapping(value="/addDrug",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject addDrug(@ApiParam(value = "药品编号",required = true) @RequestParam(value = "drug_id", required = true)int drug_id,
                              @ApiParam(value = "药品名称",required = true) @RequestParam(value = "drug_name", required = true)String drug_name,
                              @ApiParam(value = "药品规格",required = false) @RequestParam(value = "drug_capacity", required = false)String drug_capacity,
                              @ApiParam(value = "药品单位",required = false) @RequestParam(value = "drug_unit", required = false)String drug_unit,
                              @ApiParam(value = "药品单价",required = true) @RequestParam(value = "drug_price", required = true)float drug_price,
                              @ApiParam(value = "生产厂商",required = false) @RequestParam(value = "drug_manufacturer", required = false)String drug_manufacturer,
                              @ApiParam(value = "批准文号",required = true) @RequestParam(value = "drug_approval_number", required = true)String drug_approval_number,
                              @ApiParam(value = "所属医院",required = true) @RequestParam(value = "hospital", required = true)String hospital,
                              @ApiParam(value = "是否含麻黄碱",required = true) @RequestParam(value = "is_ephedrine", required = true)int is_ephedrine,
                              @ApiParam(value = "是否为处方药",required = true) @RequestParam(value = "is_prescription", required = true)int is_prescription,
                              @ApiParam(value = "配送方式",required = true) @RequestParam(value = "delivery", required = true)String delivery){
        JSONObject jsonObject = new JSONObject();
        if(drugService.selectDrugByDrugId(drug_id) != null) {
            jsonObject.put("result", 1); //该药品编号已存在
        }
        else{
            Drug drug = new Drug();
            drug.setDrug_id(drug_id);
            drug.setDrug_name(drug_name);
            drug.setDrug_capacity(drug_capacity);
            drug.setDrug_unit(drug_unit);
            drug.setDrug_price(drug_price);
            drug.setDrug_manufacturer(drug_manufacturer);
            drug.setDrug_approval_number(drug_approval_number);
            drug.setHospital(hospital);
            drug.setIs_ephedrine(is_ephedrine);
            drug.setIs_prescription(is_prescription);
            drug.setDelivery(delivery);
            drugService.addDrug(drug);
            jsonObject.put("result", 0); //新增成功
            jsonObject.put("drug",drug);
        }
        return jsonObject;
    }

    @ApiOperation(value = "删除药品",notes = "根据药品编号删除药品")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "删除成功")})
    @RequestMapping(value="/deleteDrug",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject deleteDrug(@ApiParam(value = "药品编号",required = true) @RequestParam(value = "drug_id", required = true)int drug_id){
        JSONObject jsonObject = new JSONObject();
        drugService.deleteDrug(drug_id);
        return jsonObject;
    }

    @ApiOperation(value = "药品详情",notes = "根据药品编号查询药品详情")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectDrugByDrugId",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectDrugByDrugId(@ApiParam(value = "药品编号",required = true) @RequestParam(value = "drug_id",required = true)int drug_id) {
        JSONObject jsonObject = new JSONObject();
        Drug drug = drugService.selectDrugByDrugId(drug_id);
        if(drug != null){
            jsonObject.put("drug",drug);
        }
        else{
            jsonObject.put("drug",0);
        }
        return jsonObject;
    }

    @ApiOperation(value = "药品修改",notes = "根据药品编号修改药品信息")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "修改成功")})
    @RequestMapping(value="/updateDrug",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject updateDrug(@ApiParam(value = "药品编号",required = true) @RequestParam(value = "drug_id", required = true)int drug_id,
                                 @ApiParam(value = "药品名称",required = true) @RequestParam(value = "drug_name", required = true)String drug_name,
                                 @ApiParam(value = "药品规格",required = true) @RequestParam(value = "drug_capacity", required = true)String drug_capacity,
                                 @ApiParam(value = "药品单位",required = true) @RequestParam(value = "drug_unit", required = true)String drug_unit,
                                 @ApiParam(value = "药品单价",required = true) @RequestParam(value = "drug_price", required = true)float drug_price,
                                 @ApiParam(value = "生产厂商",required = true) @RequestParam(value = "drug_manufacturer", required = true)String drug_manufacturer,
                                 @ApiParam(value = "批准文号",required = true) @RequestParam(value = "drug_approval_number", required = true)String drug_approval_number,
                                 @ApiParam(value = "所属医院",required = true) @RequestParam(value = "hospital", required = true)String hospital,
                                 @ApiParam(value = "是否含麻黄碱",required = true) @RequestParam(value = "is_ephedrine", required = true)int is_ephedrine,
                                 @ApiParam(value = "是否为处方药",required = true) @RequestParam(value = "is_prescription", required = true)int is_prescription,
                                 @ApiParam(value = "配送方式",required = true) @RequestParam(value = "delivery", required = true)String delivery){
        JSONObject jsonObject = new JSONObject();
        Drug drug = new Drug();
        drug.setDrug_id(drug_id);
        drug.setDrug_name(drug_name);
        drug.setDrug_capacity(drug_capacity);
        drug.setDrug_unit(drug_unit);
        drug.setDrug_price(drug_price);
        drug.setDrug_manufacturer(drug_manufacturer);
        drug.setDrug_approval_number(drug_approval_number);
        drug.setHospital(hospital);
        drug.setIs_ephedrine(is_ephedrine);
        drug.setIs_prescription(is_prescription);
        drug.setDelivery(delivery);
        drugService.updateDrug(drug);
        jsonObject.put("drug",drug);
        return jsonObject;
    }

    @ApiOperation(value = "药品统计",notes = "根据生产厂商、搜索词查询药品消耗信息")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectDrugConsume",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectDrugConsume(@ApiParam(value = "生产厂商",required = false) @RequestParam(value = "manufacturer", required = false)String manufacturer,
                                        @ApiParam(value = "查询词",required = false) @RequestParam(value = "searchword", required = false)String searchword) {
        JSONObject jsonObject = new JSONObject();
        List list = drugService.selectDrugConsume(manufacturer,searchword);
        float total_price = 0;
        //初始化map对象，存放从list中取出的map集合
        Map map = new HashMap();
        //初始化object对象，存放从map中出的value
        Object object = new Object();
        for(int i = 0;i < list.size();i ++) {
            map = (Map)list.get(i);
            object = map.get("total_price");
            total_price = total_price + Float.parseFloat(object.toString());
        }
        jsonObject.put("list",list);
        jsonObject.put("total_num",list.size());
        jsonObject.put("total_price",total_price);
        return jsonObject;
    }

}